﻿using Shop.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WxApi.ApiResult;

namespace Shop.TestPay
{
    public partial class ResultNotifyPage : System.Web.UI.Page
    {
        // public static string Error { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            WxApi.ApiPay.ResultNotify result = new WxApi.ApiPay.ResultNotify(this);
            var data = result.GetNotifyData();
            string return_code = string.Format("{0}", data.GetValue("return_code"));
            // System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("支付回调获取消息1:" + return_code, "WEIXINPAY");//记录日志功能
            if (return_code == "SUCCESS")
            {
                double total_fee = data.GetValue("total_fee").GetVal_Double();
                string transaction_id = data.GetValue("transaction_id").GetVal_String();
                string out_trade_no = data.GetValue("out_trade_no").GetVal_String();
                string openid = data.GetValue("openid").GetVal_String();
                DateTime time_end = WxApi.HandleTime.GetStrTime(data.GetValue("time_end").GetVal_String());
                string bank_type = data.GetValue("bank_type").GetVal_String();
                string fee_type = data.GetValue("fee_type").GetVal_String();
                StringBuilder sql = new StringBuilder();
                sql.Append(string.Format(@"
            DECLARE @count INT
            SELECT @count=COUNT(1) from dbo.SoPreWxPay WHERE cSOCode='{0}'", out_trade_no));
                sql.Append(string.Format(@"
            IF(ISNULL(@count,0)=0)
            BEGIN
            	INSERT INTO dbo.SoPreWxPay
            	        ( cSOCode ,transaction_id ,time_end ,openid , bank_type ,fee_type ,total_fee ,dDate)
            	VALUES  ( '{0}' ,'{1}' ,'{2}','{3}' ,'{4}' , '{5}' ,{6},CONVERT(varchar(100), GETDATE(), 23))
            END",
          out_trade_no, transaction_id, time_end, openid, bank_type, fee_type, total_fee));
                System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("支付回调获取消息3:" + sql, "WEIXINPAY");//记录日志功能
                HandleSQL.getObj(sql.ToString());
            }
            System.Reflection.MethodBase.GetCurrentMethod().WriteErrLog("支付回调获取消息2:" + data.ToPrintStr(), "WEIXINPAY");//记录日志功能
            result.ProcessNotify();

        }
    }
}